package RequestHeaders;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;

/** Shows all the request headers sent on the current request.
 *  <P>
 *  Taken from Core Servlets and JavaServer Pages 2nd Edition
 *  from Prentice Hall and Sun Microsystems Press,
 *  http://www.coreservlets.com/.
 *  &copy; 2003 Marty Hall; may be freely used or adapted.
 */

public class ShowRequestHeaders extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Servlet Example: Showing Request Headers";
    
    // Diapositiva 24
//    // -------------------
//    String message;
//    // Assume for simplicity that Mozilla and IE are
//    // the only two browsers
//    String userAgent = request.getHeader("User-Agent");
//    if ((userAgent != null) && (userAgent.indexOf("MSIE") != -1)) {
//    	title = "Microsoft IE!";
//    	message = "Texto de prueba" +"en caso de que IE.";
//    } else {
//    		title = "Mozilla!";
//    		message = "Texto de prueba " +"en caso de que Mozilla";
//    }
//    // -------------------
    
    // Diapositiva 26
    // ----------------
    String referer = request.getHeader("Referer");
    if (referer == null) {
    	referer = "<I>none</I>";
    }
    title = "Referring page: " + referer;
    String imageName;
    if (contains(referer, "JRun")) {
    	imageName = "Jrun.gif";
    } else if (contains(referer, "Resin")) {
    	imageName = "wasp1.gif";
    } else {
    	imageName = "nothing.gif";
    }
    String imagePath = "RequestHeaders/"+imageName;
    String docType =
    	"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
    	"Transitional//EN\">\n";
    	out.println(docType +
    	"<HTML>\n" +
    	"<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
    	"<BODY BGCOLOR=\"#FDF5E6\">\n" +
    	"<CENTER><H2>" + title + "</H2>\n" +
    	"<IMG SRC=\"" + imagePath + "\"/>\n" +
    	"</CENTER></BODY></HTML>");
    	}
    // ----------------
//    String docType =
//      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
//      "Transitional//EN\">\n";
//    out.println(docType +
//                "<HTML>\n" +
//                "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" +
//                "<BODY BGCOLOR=\"#FDF5E6\">\n" +
//                "<H1 ALIGN=\"CENTER\">" + title + "</H1>\n" +
//                message+"<BR>\n"+
//                "<B>Request Method: </B>" +
//                request.getMethod() + "<BR>\n" +
//                "<B>Request URI: </B>" +
//                request.getRequestURI() + "<BR>\n" +
//                "<B>Request Protocol: </B>" +
//                request.getProtocol() + "<BR><BR>\n" +
//                "<TABLE BORDER=1 ALIGN=\"CENTER\">\n" +
//                "<TR BGCOLOR=\"#FFAD00\">\n" +
//                "<TH>Header Name<TH>Header Value");
//    Enumeration headerNames = request.getHeaderNames();
//    while(headerNames.hasMoreElements()) {
//      String headerName = (String)headerNames.nextElement();
//      // Diapositiva 22
//      // --------------
//      if(headerName.equalsIgnoreCase("accept")){
//    	  out.println("<TR><TD>" + headerName);
//    	  out.println("    <TD>" + request.getHeader(headerName));
//    	  out.println("<TR><TD>" + "Request Method");
//    	  out.println("    <TD>" + request.getMethod());
//    	  break;
//      }
//      // --------------
//    }
//    out.println("</TABLE>\n</BODY></HTML>");
//  }

  /** Since this servlet is for debugging, have it
   *  handle GET and POST identically.
   */
  
  public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }
  
  private boolean contains(String mainString, String subString) {
	  return(mainString.indexOf(subString) != -1);
  }
}
